/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.iamk.database;
import java.sql.Connection;
import java.sql.DriverManager;

import com.iamk.util.ResourceBundleUtil;


public class ConnectionFactory {

    private static ResourceBundleUtil resourceBundleUtil = new ResourceBundleUtil("SERVERCONFIG");

    public enum DBType {

        ORACLE, DB2, SQLSERVER, MYSQL
    }
    private static ETT_DBConnect connect;
    private static DBType mstrDBType = DBType.ORACLE; // 2. Default is Oracle

    private static void getConnectConfig() {
        connect = new ETT_DBConnect();
        connect.mstrDBType = DBType.valueOf(resourceBundleUtil.getObjectAsString("AMType"));
        connect.mstrUserName = resourceBundleUtil.getObjectAsString("AMUser");
        connect.mstrPassWord = resourceBundleUtil.getObjectAsString("AMPass");
        String strHost = resourceBundleUtil.getObjectAsString("AMHost");
        String strPort = resourceBundleUtil.getObjectAsString("AMPort");
        String strSID = resourceBundleUtil.getObjectAsString("AMSid");
        switch (connect.mstrDBType) {
            case DB2: {
                // 1. DB2 connection string
            }
            case ORACLE: {
//                connect.mstrURL = "jdbc:oracle:thin:@" + strHost + ":" + strPort + ":" + strSID;
            }
            case SQLSERVER: {
                // 2. SQLSERVER connection string
            }
            case MYSQL: {
                connect.mstrURL = "jdbc:mysql://" + strHost + ":" + strPort + "/" + strSID + "?characterEncoding=UTF-8";
            }
        }
    }

    public static Connection getConnection() throws Exception {
        getConnectConfig();
        switch (connect.mstrDBType) {
            // 1. Oracle database
            case ORACLE: {
//                DriverManager.registerDriver(new oracle.jdbc.OracleDriver());
//                return DriverManager.getConnection(connect.mstrURL, connect.mstrUserName, connect.mstrPassWord);
            }

            // 2. DB2 database
            case DB2: {
                // 2.1 DB2Connection here
                return null;
            }
            // 3. SQLSERVER database
            case SQLSERVER: {
                // 3.1 SQLSERVERConnection here
                return null;
            }
            case MYSQL: {
                Class.forName("com.mysql.jdbc.Driver");
                return DriverManager.getConnection(connect.mstrURL, connect.mstrUserName, connect.mstrPassWord);
            }
            // 4. Other database here
            default: {
                return null;
            }
        }
    }
}

class ETT_DBConnect {

    public String mstrURL;
    public String mstrUserName;
    public String mstrPassWord;
    public int miMaxConnection;
    public int miMinConnection;
    public ConnectionFactory.DBType mstrDBType;
}
